 
//  ReceiveViewController.m
//  PT
//
//  Created by jarze on 15/6/9.
//  Copyright (c) 2015年 jarze. All rights reserved.
//

#import "ReceiveViewController.h"

#import "BasicTableViewController.h"


#import "MeViewController.h"
#import "PTReceiveCountry.h"
#import "MMPickerView.h"
@interface ReceiveViewController ()
@property (nonatomic, strong) NSArray *countrys;
@property (nonatomic, strong) NSArray *stringsArray;
@property (nonatomic, strong) NSArray *countryModels;
@property (nonatomic, strong) NSString * selectedString;
@end

@implementation ReceiveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [_countryButton.layer setBorderWidth:1.0];
//    [_countryButton.layer setCornerRadius:3.0];
    self.next.enabled = NO;
    self.telephone.enabled = NO;
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(textChangeField) name:UITextFieldTextDidChangeNotification object:self.telephone];
    //以下为测试用代码，完成后请直接删除
    self.next.enabled = YES;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)textChangeField
{
    if([NSString checkTelNumber:self.telephone.text]) {
        self.next.enabled = YES;
    } else {
        self.next.enabled = NO;

    }
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
  //  UITableViewController *data = segue.destinationViewController;
//    data.tableView.dataSource = self;
//    data.tableView.delegate = self;
   
    
    
}




- (IBAction)nextOnClick:(id)sender {
    
    
  //  DataViewController *data = [[DataViewController alloc] init];
//    
//    ChooseTeacherViewController *data = [[ChooseTeacherViewController alloc] init];
//    
//    // MeViewController *data = [[MeViewController alloc] init];
//    
//  //  CTViewController *data = [[CTViewController alloc] init];
//    
//  // DataTableViewController *data = [ alloc] init];
//    
  //  [self.navigationController pushViewController:data animated:YES];



}



- (IBAction)countySelect:(UIButton *)sender
{
    _stringsArray = [self countrys];
    _selectedString = [[self countrys] objectAtIndex:0];

    [MMPickerView showPickerViewInView:self.view withStrings:_stringsArray withOptions:@{
                                                              MMtoolbarBackgroundImage: [UIImage imageNamed:@"Title@2x"],
                                                                     MMbackgroundColor: [UIColor whiteColor],
                                                                        MMtoolbarColor: [UIColor clearColor],
                                                                                MMfont: [UIFont systemFontOfSize:18],
                                                                              MMvalueY: @3,
                                                                      MMselectedObject:_selectedString,
                                                                       MMtextAlignment:@1}
                            completion:^(NSString *selectedString) {
                                [self.countryButton setTitle:selectedString forState:UIControlStateNormal];
                                self.number.text = [self getNumber:selectedString];
                                
                                self.telephone.enabled = YES;
                                [self.telephone becomeFirstResponder];

                            }];

}

- (NSString *)getNumber:(NSString *)countryButton {
    NSString *number = [NSString string];
    for(PTReceiveCountry *country in self.countryModels) {
        if ([country.name isEqualToString:countryButton]) {
            number = country.number;
            break;
        }
    }
    return  number;
}
- (NSArray *)countrys
{
    if(_countrys == nil) {
        NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"country" ofType:@"plist"]];
        NSMutableArray *models = [NSMutableArray arrayWithCapacity:array.count];
        NSMutableArray *modelss = [NSMutableArray arrayWithCapacity:array.count];
        for(NSDictionary *dict in array) {
            PTReceiveCountry *flag = [PTReceiveCountry countryWithDictionary:dict];

            [modelss addObject:flag];
            [models addObject:flag.name];
        }
        _countrys = models;
        _countryModels = modelss;
    }
    return _countrys;
}
@end
